<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiEncoder
{
	protected $_key = '';
	
	protected $_key_assoc = array();
	
	protected $_key_length = 0;		
			
	
	public function __construct()
	{
		$this->_key = md5(Rti::config('config')->get('ENCODER_KEY'));

		$this->_key_assoc  = str_split($this->_key);
		
		$this->_key_length = count($this->_key_assoc);				
	}
	
	protected function _xor_algorithm($text) 
	{
		$result     = '';				
		$key_index  = -1;		
		$text_assoc = str_split($text);	
		
		foreach ($text_assoc as $char_text) 
		{
			$key_index++;	
					
			if ($key_index >= $this->_key_length) 
			{
				$key_index = 0;	
			}
			
			$char_key = $this->_key_assoc[$key_index];	
					
			$result .= chr(ord($char_text) ^ ord($char_key));			
		}		
		
		return $result;		
	} 

	public function encode($text) 
	{
		return base64_encode(self::_xor_algorithm($text));
	}	
	
	public function decode($text) 
	{
		return self::_xor_algorithm(base64_decode($text));
	}

}

/* End of file RtiEncoder.php */
/* Location: ./system/core/RtiEncoder.php */

